package com.topfinance.payment.service.impl;

import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.payment.ebo.*;
import com.topfinance.payment.service.*;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * 
 * @author
 * 
 */

public class TCfgFmtEleMapMgrImpl extends HibernateEntityDao<TC1BisEleBscEbo> implements ITCfgFmtEleMapMgr {

	private static final Log log = LogFactory.getLog(TCfgFmtEleMapMgrImpl.class);

	// 根据报文号、业务类型号、业务种类号条件映射定义
	public List<TCfgFmtEleMapFileEbo> getAllFmtEleMap(Integer fmtID, String msg, String tpCode, String clsCode)
			throws AppException {

		// TODO:无法正常判断为空条件
		if (StringUtil.isNullInteger(fmtID))
			return null;

		// 获得映射定义
		String hql = " from TCfgFmtEleMapFileEbo t ";
		String where = "";
		if (StringUtil.isNotNullStr(msg))
			where += " and t.msgCode = '" + msg + "'";
		// TODO:
		if (StringUtil.isNotNullStr(tpCode))
			where += " and t.tpCode = '" + tpCode + "'";
		else
			where += " and t.tpCode is null";

		// TODO:
		if (StringUtil.isNotNullStr(clsCode))
			where += " and t.clsCode = '" + clsCode + "'";
		else
			where += " and t.clsCode is null";
		if (fmtID != null && fmtID.intValue() > 0)
			where += " and t.tCfgFormat.oid = " + fmtID;

		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		return find(hql, paramList.toArray());
	}

	// 根据mapID获得映射规则定义
	public List<TCfgFmtEleMapRuleEbo> getAllFmtEleMap(Integer fmtID) throws AppException {
		if (StringUtil.isNullInteger(fmtID))
			return null;

		// 获得映射规则定义
		String hql = " from TCfgFmtEleMapRuleEbo t ";
		String where = "";
		where += " and t.tCfgFormat.oid = " + fmtID;
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		return find(hql, paramList.toArray());
	}

	// 保持新定义的
	public void saveMapRule(TCfgFmtEleMapFileEbo map, List<Integer> detailsToDelete) throws AppException {
		if (detailsToDelete != null && detailsToDelete.size() > 0) {
			Map<Integer, Integer> maps = new HashMap<Integer, Integer>();
			for (Integer id : detailsToDelete) {
				maps.put(id, id);
				removeById(TCfgFmtEleMapRuleEbo.class, id);
			}

			for (Iterator<TCfgFmtEleMapRuleEbo> it = map.getMappings().iterator(); it.hasNext();) {
				TCfgFmtEleMapRuleEbo rule = it.next();
				if (maps.containsKey(rule.getId())) {
					it.remove();
				}
			}
		}
		saveOrUpdate(map);
		Set<TCfgFmtEleMapRuleEbo> rules = map.getMappings();
		for(TCfgFmtEleMapRuleEbo rule:rules){
			rule.setMap(map);
			saveOrUpdate(rule);
		}
	}

    public TCfgFmtEleMapFileEbo getMapRule(Integer fmtID,String msgCode, String tpCode, String clsCode) throws AppException {
        String hql = " from TCfgFmtEleMapFileEbo t " ;
        //String where = " where t.msgCode= ? and t.tpCode = ? and t.clsCode= ?";
        String where = "";
        if(fmtID != null &&fmtID.intValue() > 0)
        	where += " and t.TCfgFormat.oid = " + fmtID;
        //else
        	//where += " and t.tCfgFormat is null";
        if(StringUtil.isNotNullStr(msgCode))
        	where += " and t.msgCode ='" + msgCode + "'";
        else
        	where += " and t.msgCode is null";
        if(StringUtil.isNotNullStr(tpCode))
        	where += " and t.tpCode ='" + tpCode + "'";
        else
        	where += " and t.tpCode is null";
        if(StringUtil.isNotNullStr(clsCode))
        	where += " and t.clsCode ='" + clsCode + "'";
        else
        	where += " and t.clsCode is null";
		if (isNotBlank(where)) {
			where = " where " + where.substring(4);
		}
		hql = hql + where;
        List list = find(hql);
        if(list.size()>1) {
            //throw new AppException("more than one rule with mesgCode=" + msgCode + ", tpCode="+tpCode+", clsCode="+clsCode);
        	//默认返回第一个
        	return (TCfgFmtEleMapFileEbo)list.get(0);
        }
        return list.size()==1 ? (TCfgFmtEleMapFileEbo)list.get(0) : null;
    }
	
}
